package com.sharecode.stock.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.sharecode.stock.aop.ParamData;
import com.sharecode.stock.exception.StockException;
import com.sharecode.stock.repository.StockStrategyRepository;
import com.sharecode.stock.utils.DateUtils;
import com.sharecode.stock.vo.stock.StockStrategy;

@Service
public class StockStrategyService {

	@Autowired
	private StockStrategyRepository repository;

	@Autowired
	private StockHistoryDateService stockHistoryDateService;

	public void update(Long id, int star) {
		StockStrategy strategy = repository.findById(id).get();
		strategy.setStar(strategy.getStar() + star);
		if (strategy.getStar() > 10 || strategy.getStar() < 0) {
			throw new StockException("推荐指数0-10星");
		}
		repository.saveAndFlush(strategy);
	}

	public void save(StockStrategy stockStrategy) {
		repository.save(stockStrategy);
	}

	public void save(List<StockStrategy> stockStrategies) {
		repository.saveAll(stockStrategies);
	}

	public Page<StockStrategy> findPage(ParamData param) {
		Sort sort = Sort.by(Order.desc("date"), Order.desc("id"));

		PageRequest pageable = PageRequest.of(param.getPage() - 1, param.getSize(), sort);

		Specification<StockStrategy> spec = new Specification<StockStrategy>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<StockStrategy> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<>();
				Predicate predicate = criteriaBuilder.equal(root.get("cycle"), 1);
				predicates.add(predicate);
				String code = param.getCode();
				if (StringUtils.isNotBlank(code)) {
					predicates.add(criteriaBuilder.equal(root.get("code"), code));
				}
				return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		return repository.findAll(spec, pageable);
	}

	/**
	 * 查找推荐的
	 * 
	 * @param param
	 * @return
	 */
	public List<StockStrategy> findRm() {

		String date = DateUtils.day(-1, stockHistoryDateService.find().getDate(), DateUtils.FORMAT_YYYY_MM_DD);

		Sort sort = Sort.by(Order.desc("star"));

		PageRequest pageable = PageRequest.of(0, 10, sort);
		Specification<StockStrategy> spec = new Specification<StockStrategy>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<StockStrategy> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				Predicate predicate = criteriaBuilder.equal(root.get("date"), date);
				return predicate;
			}
		};
		return repository.findAll(spec, pageable).getContent();
	}
}
